window.onload = function () {
    const {createEditor, createToolbar} = window.wangEditor

    const editorConfig = {
        placeholder: 'Type here...',
        onChange(editor) {
            const html = editor.getHtml()
            console.log('editor content', html)
            // 也可以同步到 <textarea>
        },
    }

    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'default', // or 'simple'
    })

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    })

    $('#submit-btn').click(function (event) {
        //阻止按钮的默认行为（表单方式提交）
        event.preventDefault()
        let title = $("input[name='title']").val();
        let categoryStr = $("select[name='category']").val();
        // 使用parseInt()将字符串转换为整数
        let category = parseInt(categoryStr, 10);
        let content = editor.getHtml();
        let csrfmiddlewaretoken = $('input[name="csrfmiddlewaretoken"]').val();
        $.ajax(url = '', options = {
            method: 'POST',
            data: {
                title, category, content, csrfmiddlewaretoken  // 防止 CSRF 攻击
            },
            success: function (result) {
                if (result['code'] == 200) {
                    var blog_id = result['data']['blog_id'];
                    // 跳转到博客详情页
                    window.location.href = '/app1/detail/' + blog_id;  // 跳转到博客详情页
                }else{
                    alert(result['msg'])
                }
            }
        })

    });

}